
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author BENSON
 */
public class ControllerModifyFlight {
    
      Connection conn=null;
    String deleteStudent;
    String querry;
    Statement st=null;
   public ModifyFlight modFlt;
    
    private ActionListener actionListener;
    
    public ControllerModifyFlight( ){
       // this.deleteOrder = delete;
        modFlt = new ModifyFlight();
        modFlt.setVisible(true);
                          
    }
   
     
    public void showOrders(){
        String ky ="";
         ky = modFlt.getKey().getText();
    try{
        Class.forName("com.mysql.jdbc.Driver");
        //jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull [root on Default schema]
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","root");
        querry="update flights set fName =?,ffrom=?,destination=?,Price=?,fCapacity=?,depatureTime=?,arrivalTime=?" 
                + "where fName like '%" + ky+"%'";
        st=conn.prepareStatement(querry);
        PreparedStatement stmt = conn.prepareStatement(querry);
        st.execute("USE jetwingdb;");

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
   stmt.setString(1, modFlt.getFname().getText());
   stmt.setString(2, modFlt.getFname().getText());
   stmt.setString(3, modFlt.getFromtext().getText());
   stmt.setString(4, modFlt.getDesttext().getText());
   stmt.setString(5, modFlt.getCosttext().getText());
   stmt.setString(6, modFlt.getDepttext().getText());
   stmt.setString(7, modFlt.getArivtext().getText());
   
   }
   st.close();
   conn.close();
  
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    }
    
     public void listFlights(){
         
         String ky ="";
         ky = modFlt.getKey().getText();
    try{
        Class.forName("com.mysql.jdbc.Driver");
        //jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull [root on Default schema]
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","root");
        querry="SELECT * FROM Flights ;" ;
        st=conn.prepareStatement(querry);
        st.execute("USE jetwingdb;");

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
       modFlt.getKey().setText(rs.getString("fName"));
       modFlt.getFname().setText(rs.getString("fName"));
       modFlt.getFromtext().setText(rs.getString("ffrom")); 
       modFlt.getDesttext().setText(rs.getString("destination"));
       modFlt.getCosttext().setText(rs.getString("Price"));
       modFlt.getCaptext().setText(rs.getString("fCapacity"));
       modFlt.getDepttext().setText(rs.getString("depatureTime"));
       modFlt.getArivtext().setText(rs.getString("arrivalTime"));
   }
   st.close();
   conn.close();
  
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    }
     public void contol(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==modFlt.getListButton()){
                        showOrders();
                  }
                  if(actionEvent.getSource()==modFlt.getClose()){
                  modFlt.setVisible(false);
                  
                  }
              }
              
        };                
        modFlt.getSearchButton().addActionListener(actionListener);
        modFlt.getClose().addActionListener(actionListener);
        modFlt.getSaveChanges().addActionListener(actionListener); 
    }
     public static void main(String[]args){
         ControllerModifyFlight c= new ControllerModifyFlight();
         c.contol();
     
     }
    
}

